$(function () {
    //角色列表
    function getTable() {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Admin/Role?timestamp=' + _timestamp + salt);
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/Role?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                // var result = JSON.stringify(data);
                if (data.code == 1) {
                    $("#roleTable tbody tr").remove();
                    console.log(data);
                    var count = data.data.length;
                    for (var i = 0; i < count; i++) {
                        var role = data.data[i].role || '';
                        var addtime = data.data[i].addtime || '';
                        if (addtime != '') {
                            addtime = getMyDate(addtime)
                        }
                        var description = data.data[i].description || '';
                        var id = data.data[i].id || '';
                        var updatetime = data.data[i].updatetime || '';
                        if (updatetime != '') {
                            updatetime = getMyDate(updatetime)
                        }
                        var tr = "<tr _id=" + id + "><td>" + id + "</td><td>" + role + "</td><td>" + addtime + "</td><td>" + description + "</td><td>" + updatetime + "</td><td><button class='roleTable_updata' id=roleTable_updata_" + i + "><i class='icon-edit'>&nbsp;</i>修改</button><button class='roleTable_delect' id=roleTable_delect_" + i + "><i class='icon-trash'>&nbsp;</i>删除</button></td></tr>";
                        $("#roleTable tbody").append(tr);
                        updateAndDel(i);
                    }
                }
            },
            error: function () {
                alert('请求角色列表失败');
            }
        });
    }
    getTable();


//添加角色按钮
    $("#addPerson").click(function () {
        $(".add_div").fadeToggle();

    });
    $("#btnYesAd").click(function () {
        var _timestamp = new Date().getTime();
        var _secret = hex_md5(ip + '/Back/Admin/AddRole?timestamp=' + _timestamp + salt);
        var _role = $("#role").val();
        var _description = $("#description").val();
        var _roleToPermission = $("#roleToPermission").val();
        $.ajax({
            type: "POST",
            url: ip + '/Back/Admin/AddRole?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
            dataType: 'json',
            data: JSON.stringify({
                "description": _description,
                "role": _role,
                "roleToPermission": _roleToPermission
            }),
            headers: {
                "Content-Type": "application/json",
                "Accept": "application/json;charset=utf-8",
            },
            xhrFields: {withCredentials: true},
            success: function (data) {
                if (data.code==1){
                    alert("添加成功");
                    getTable();
                    $(".add_div").fadeOut();
                }
                else {
                    alert(data.message)
                }
            },
            error: function () {
                alert('添加失败');
            },
        });
    });
    $("#btnNoAd").click(function () {
        $(".add_div").fadeOut();
    });

    // 添加 修改角色
    function updateAndDel(i) {
        // 修改角色
        $("#roleTable_updata_" + i).click(function () {
            var _timestamp = new Date().getTime();
            var _secret = hex_md5(ip + '/Back/Admin/DelRole?timestamp=' + _timestamp + salt);
            $.ajax({
                type: "get",
                url: ip + '/Back/Admin/UpdateRole?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
                dataType: 'json',
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json;charset=utf-8",
                },
                data: JSON.stringify({}),
                success: function (data) {
                    // var result = JSON.stringify(data);
                    console.log(data)
                },
                error: function () {
                    alert('修改失败');
                },
            });
        });
        //删除角色
        $("#roleTable_delect_" + i).click(function () {
            var _timestamp = new Date().getTime();
            var _secret = hex_md5(ip + '/Back/Admin/DelRole?timestamp=' + _timestamp + salt);
            var _id = $(this).parent().parent().attr("_id");
            var that = $(this).parent().parent();
            $.ajax({
                type: "POST",
                url: ip + '/Back/Admin/DelRole?timestamp=' + _timestamp + '&secret=' + _secret + '&token=' + _token,
                dataType: 'json',
                data: JSON.stringify({"id": _id}),
                headers: {
                    "Content-Type": "application/json",
                    "Accept": "application/json;charset=utf-8",
                },
                success: function (data) {
                    // var result = JSON.stringify(data);
                    if(data.code==1){
                        that.fadeOut();
                    }
                    else {
                        alert(dada.message)
                    }
                 },
                error: function () {
                    alert('删除失败');
                },
            });
        })
    }

})